import { sendMsgToBackground } from "../utils/chrome";

const baseApi = async params => {
  console.log("baseApi", params);
  return new Promise((rs, rj) => {
    try {
      sendMsgToBackground({ action: "request-api", data: params }, result => {
        console.log("request-api result", result);
        const { _success, _msg } = result;
        if (!_success) {
          console.error(_msg);
          rj(_msg || "操作失败");
          return;
        }
        rs(result);
      });
    } catch (e) {
      console.error(e);
      rj({ code: -1, _success: false, _msg: (e && e.message) || e });
    }
  });
};

export default baseApi;
